// Digit指示数字/符号, Img理所当然是图片啦.
function ScoreDigit(pV,bR, DigiT, ImG)
{
	var _this = this;
	Sprite.call(_this, pV, bR);
	
	_this.digit = undefined;
	_this.img = undefined;
	
	_this.draw = function(canvas)
	{
		var context = canvas.getContext("2d");
		context.drawImage(_this.img, _this.position.x, _this.position.y);
	}
	
	var init = function()
	{
		_this.digit = DigiT;
		_this.img = ImG;
	}
	init();
}

function ScoreBoard(pV, bR, ScalE)
{
	var _this = this;
	Sprite.call(_this, pV, bR);
	
	
	_this.ready = false;
	// 用来指示板子大小的变量 范围从 0-1;
	_this.scale = 1;
	// 记录分数用
	_this.score = 0;
	
	_this.imgSrc = ["./images/num0.png",
					 "./images/num1.png",
					 "./images/num2.png",
					 "./images/num3.png",
					 "./images/num4.png",
					 "./images/num5.png",
					 "./images/num6.png",
					 "./images/num7.png",
					 "./images/num8.png",
					 "./images/num9.png"
					 ];
	// 我用上面定义好的数字类来做.
	_this.digitList = undefined;
	
	_this.imgCount = undefined;
	
	_this.digitPump = undefined;
	
	// 更新的时候传个数进来就是了.
	_this.update = function(mark){
		_this.score = mark;	
	}
	
	_this.draw = function(canvas){
		var cxt = canvas.getContext("2d");
		var s = parseInt(_this.score);
		
		// 相当于把digitPump(泵)用来当数字暂存器,
		// 如果分数是 123, 那么digitPump里存的就是 [3, 2, 1]
		_this.digitPump = []; // 清空队列, 放心, 浏览器会自动回收垃圾的.//其实每次跑的时候它都是空的.
		while(s != 0)
		{
			_this.digitPump.push(parseInt(s % 10));
			s = parseInt(s / 10);
		}
		
		
		// 保存当前状态, 不影响其他Sprite的绘图
		cxt.save(); // 保存第一次:缩放状态,
			cxt.scale(_this.scale, _this.scale);
			cxt.translate(_this.position.x, _this.position.y);
			cxt.save(); // 保存第二次, 用于移动到下一个数字, 继续画
				while(_this.digitPump.length > 0)
				{
					var index = _this.digitPump.pop();
					_this.digitList[index].draw(canvas);
					// 移动的步长:
					var step = _this.digitList[index].box.width;
					cxt.translate(step,0);
				}
			cxt.restore();
		cxt.restore();
		
	}
	
	_this.imgLoadCallBack = function()
	{
		// 如果所有图片加载完成, 那么设置ready为真
		_this.imgCount += 1;

		if(_this.imgCount == _this.imgSrc.length)
		{
			_this.ready = true;
		}
	}

	var init = function()
	{
		_this.digitList = new Array();
		_this.imgCount = 0;
		_this.digitPump = new Array();
		for(var i = 0; i < _this.imgSrc.length; ++i)
		{
			var img = new Image();
			img.src = _this.imgSrc[i];
			img.onload = _this.imgLoadCallBack;
			var dgt = new ScoreDigit(new Vector2(0,0),new Rectangle(54,56),i,img);
			_this.digitList.push(dgt);
		}
		
		_this.scale = ScalE;
	}
	init();
}// JavaScript Document